/// Copyright (c) Microsoft Corporation.  All rights reserved.
/// 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Microsoft.VSPowerToys.PackInstaller
{
    public partial class HorizontalRuleControl : UserControl
    {
        private Color colorFrom;

        /// <summary>
        /// This property sets the starting color for the gradient.
        /// </summary>
        public Color ColorFrom
        {
            get { return colorFrom; }
            set
            {
                colorFrom = value;
                RefreshGradient();
            }
        }

        private Color colorTo;

        /// <summary>
        /// This property sets the ending color for the gradient.
        /// </summary>
        public Color ColorTo
        {
            get { return colorTo; }
            set
            { 
                colorTo = value;
                RefreshGradient();
            }
        }

        private LinearGradientBrush horizontalRuleBrush;

        /// <summary>
        /// Constructor, this defaults the colors to fade from the dark control color to the background color.
        /// </summary>
        public HorizontalRuleControl()
        {
            InitializeComponent();

            this.colorFrom = SystemColors.ControlDark;
            this.colorTo = this.BackColor;
            RefreshGradient();
        }

        /// <summary>
        /// Paint event handler, fills in the client rectangle with the defined gradient.
        /// </summary>
        /// <param name="sender" />
        /// <param name="e" />
        private void HorizontalRuleControl_Paint(object sender, PaintEventArgs e)
        {
            if (this.horizontalRuleBrush != null)
            {
                e.Graphics.FillRectangle(this.horizontalRuleBrush, this.ClientRectangle);
            }
        }

        /// <summary>
        /// Resize event handler, refreshes the gradient
        /// </summary>
        /// <param name="sender" />
        /// <param name="e" />
        private void HorizontalRuleControl_SizeChanged(object sender, EventArgs e)
        {
            RefreshGradient();
        }

        /// <summary>
        /// Size property changing event handler, refreshes the gradient,
        /// connected to the Parent control so when the parent's size changes we can change as well.
        /// </summary>
        /// <param name="sender" />
        /// <param name="e" />
        private void Parent_SizeChanged(object sender, EventArgs e)
        {
            RefreshGradient();
        }

        /// <summary>
        /// Event that gets triggered when the parent property is changed.  We use this to hook up an event
        /// for the parent resize event.
        /// </summary>
        /// <param name="sender" />
        /// <param name="e" />
        private void HorizontalRuleControl_ParentChanged(object sender, EventArgs e)
        {
            if (this.Parent != null)
            {
                this.Parent.SizeChanged += new EventHandler(Parent_SizeChanged);
            }
        }

        /// <summary>
        /// Defines the gradient we want to use for filling in the control, the gradient will fill the client
        /// area.
        /// </summary>
        public void RefreshGradient()
        {
            if (this.ClientRectangle.Width > 0 && this.ClientRectangle.Height > 0)
            {
                this.horizontalRuleBrush = new LinearGradientBrush(this.ClientRectangle, this.colorFrom, this.colorTo, 0.0f);
                this.Refresh();
            }
            else
            {
                this.horizontalRuleBrush = null;
            }
        }
    }
}
